package net.trade.service.trade;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class HttpTrade {

	public static boolean sendPostRequest(String path, String params, String enc)
			throws Exception {
		// title=dsfdsf&timelength=23&method=save
		StringBuilder sb = new StringBuilder();
		params = URLEncoder.encode(params, enc);
		byte[] entitydata = params.getBytes();// 得到实体的二进制数据
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		conn.setDoOutput(true);// 如果通过post提交数据，必须设置允许对外输出数据
		// 至少要设置的两个请求头
		// Content-Type: application/x-www-form-urlencoded //内容类型
		// Content-Length: 38 //实体数据的长度
		conn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");//
		// 长度是实体的二进制长度
		conn.setRequestProperty("Content-Length",
				String.valueOf(entitydata.length));

		OutputStream outStream = conn.getOutputStream();
		outStream.write(entitydata);
		outStream.flush();
		outStream.close();
		if (conn.getResponseCode() == 200) {
			String s = conn.getResponseMessage();
			System.out.println("返回："+s);
			//return true;
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));
        System.out.println("=============================");
        System.out.println("Contents of get request");
        System.out.println("=============================");
        String lines;
        while ((lines = reader.readLine()) != null) {
            System.out.println(lines);
        }
        reader.close();

		return false;
	}

}
